package cn.tedu.validationdemo.controller;

import cn.tedu.validationdemo.entity.User;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
public class UserController {

    //http://localhost:8080/login
    @RequestMapping("/login")
    //注意: 下一行的参数添加了@Valid注解
    public String login(@Valid User user ,
                        BindingResult bindingResult){
        System.out.println("user = " + user);

        //被验证的数据必须和BindingResult必须是连续的
        if (bindingResult.hasErrors()){
            String errorMessage = bindingResult.getFieldError().getDefaultMessage();
            return errorMessage;
        }else{
            return "Ok";
        }

    }
}
